package com.mariusz;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import com.mariusz.data.DataContainer;
import com.mariusz.data.dto.Property;
import com.mariusz.data.dto.Room;
import com.mariusz.exceptions.ReadDataException;
import com.mariusz.managers.DataManager;
import com.mariusz.managers.NavigationManager;
import com.mariusz.managers.NavigationManager.Screen;
import com.mariusz.panels.LandlordsListPanel;
import com.mariusz.panels.LocatorsPanel;
import com.mariusz.panels.PropertyEditPanel;
import com.mariusz.panels.RentierDetailsPanel;
import com.mariusz.panels.PropertiesPanel;
import com.mariusz.panels.RentiersListPanel;
import com.mariusz.panels.StatusPanel;
import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;

import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXPanel;

import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.NavigableMap;

import org.jdesktop.swingx.JXFrame.StartPosition;

public class Main extends JXFrame implements WindowListener {

	private JXPanel statusPanel;
	private JXPanel rentiersListPanel;
	private PropertiesPanel propertiesPanel;
	private PropertyEditPanel editPropertyPanel;
	private RentierDetailsPanel rentierDetailsPanel;
	private LocatorsPanel locatorsPanel;
	private LandlordsListPanel landlordsPanel;
	/**
	 * 
	 * Create the panel.
	 */
	public Main(){
		try {
			DataManager.getInstance().retriveData();
		} catch (Exception e) {
			DataManager.getInstance().setContainer(new DataContainer());
			DataManager.getInstance().setMockData();
		} 
		setStartPosition(StartPosition.CenterInScreen);
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/com/mariusz/resources/home_icon.gif")));
		
		setTitle("Property Manager");
		
		setBackground(Color.WHITE);
		this.setSize(700,500);
		getContentPane().setLayout(new CardLayout(0, 0));
		
		JMenuBar menuBar = new JMenuBar();
		//setJMenuBar(menuBar);
				
		JMenu mnNewMenu = new JMenu("Open");
		//menuBar.add(mnNewMenu);
		
		statusPanel = new StatusPanel();
		getContentPane().add(statusPanel,Screen.STATUS_PANEL.toString());
	
		rentiersListPanel = new RentiersListPanel();
		getContentPane().add(rentiersListPanel,Screen.RENTIER_LIST.toString());
		
		rentierDetailsPanel = new RentierDetailsPanel();
		getContentPane().add(rentierDetailsPanel,Screen.RENTIER_DETAILS.toString());
		
		propertiesPanel = new PropertiesPanel();
		getContentPane().add(propertiesPanel,Screen.PROPERTIES.toString());
		
		editPropertyPanel = new PropertyEditPanel();
		getContentPane().add(editPropertyPanel,Screen.PROPERTY_EDIT.toString());
	
		locatorsPanel = new LocatorsPanel();
		getContentPane().add(locatorsPanel,Screen.LOCATORS.toString());
		
		landlordsPanel = new LandlordsListPanel();
		getContentPane().add(landlordsPanel,Screen.LANDLORDS.toString());
		
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.addWindowListener(this);

	}

	public  static void main(String[] args){
		try {	
			NimRODTheme nt = new NimRODTheme( "blue.theme");
			NimRODLookAndFeel nf = new NimRODLookAndFeel();
			NimRODLookAndFeel.setCurrentTheme( nt);
			UIManager.setLookAndFeel( nf);
		} catch (Exception e) {
			try {
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			} catch (Exception e1) {
				
				e1.printStackTrace();
			} 
		}
		Main main = new Main();
		main.setVisible(true);
		NavigationManager.getInstance().setMainWindow(main);
		NavigationManager.getInstance().switchTo(Screen.STATUS_PANEL);
	
		
	
	
	}

	@Override
	public void windowActivated(WindowEvent e) {
	
		
	}
	

	@Override
	public void windowClosed(WindowEvent e) {
	
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		try {
			DataManager.getInstance().persistData();
			System.exit(0);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	public JXPanel getStatusPanel() {
		return statusPanel;
	}

	public JXPanel getRentiersListPanel() {
		return rentiersListPanel;
	}

	public PropertiesPanel getPropertiesPanel() {
		return propertiesPanel;
	}

	public PropertyEditPanel getEditPropertyPanel() {
		return editPropertyPanel;
	}
}
